#include "StdAfx.h"
#include ".\myatlimage.h"

CMyAtlImage::CMyAtlImage(void)
{
}

CMyAtlImage::~CMyAtlImage(void)
{
}



void CMyAtlImage::SetImageBackColor( CImage* img,COLORREF color )
{
	SetBkColor(img->GetDC(),color);
	img->ReleaseDC();
	RECT rc;
	rc.left=0;
	rc.right=img->GetWidth();
	rc.top=0;
	rc.bottom=img->GetHeight();
	::ExtTextOutA(img->GetDC(), 0, 0, ETO_OPAQUE, &rc, NULL, 0, NULL);
	img->ReleaseDC();
}

void CMyAtlImage::PNGImageAlpha( CImage* pPngImg )
{
	if (pPngImg->GetBPP()>=32)
	{
		for (int i=0;i<pPngImg->GetWidth();++i)
		{
			for (int j=0;j<pPngImg->GetHeight();++j)
			{
				unsigned char* pucColor=(unsigned char*)pPngImg->GetPixelAddress(i,j);
				pucColor[0]=pucColor[0]*pucColor[3]/255;
				pucColor[1]=pucColor[1]*pucColor[3]/255;
				pucColor[2]=pucColor[2]*pucColor[3]/255;
			}
		}
	}
}

BOOL CMyAtlImage::LoadPNG( LPCTSTR pszFileName )
{
	if (FAILED(ATL::CImage::Load(pszFileName)))
	{
		return FALSE;
	}
	PNGImageAlpha(this);

	return TRUE;
}

void CMyAtlImage::SetBackColor( COLORREF color )
{
	SetImageBackColor(this,color);
}

void CMyAtlImage::CopyPixel(CImage* pDst,CPoint pointDst,CImage* pSrc,CRect rcSrc)
{
	ASSERT(pDst!=NULL && pSrc!=NULL);

	rcSrc.right=(rcSrc.right==0)?pSrc->GetWidth():rcSrc.right;
	rcSrc.bottom=(rcSrc.bottom==0)?pSrc->GetHeight():rcSrc.bottom;
	ASSERT(!rcSrc.IsRectEmpty());

	for (int i=rcSrc.left;i<rcSrc.right;++i)
	{
		for (int j=rcSrc.top;j<rcSrc.bottom;++j)
		{
			unsigned char* pucSrc=(unsigned char*)pSrc->GetPixelAddress(i,j);
			unsigned char* pucDst=(unsigned char*)pDst->GetPixelAddress(i+pointDst.x,j+pointDst.y);
			pucDst[0]=pucSrc[0];
			pucDst[1]=pucSrc[1];
			pucDst[2]=pucSrc[2];
			pucDst[3]=pucSrc[3];
		}
	}
}

BOOL CMyAtlImage::IsHaveAlphaChannel()
{
	if (GetBPP()<=24)
	{
		return FALSE;
	}
	return TRUE;
}

BYTE CMyAtlImage::GetAlpha( CImage* pImg,int x,int y )
{
	unsigned char* pucSrc=(unsigned char*)pImg->GetPixelAddress(x,y);
	return pucSrc[3];
}

void CMyAtlImage::SetAlpha( CImage* pImg,int x,int y,BYTE a )
{
	unsigned char* pucSrc=(unsigned char*)pImg->GetPixelAddress(x,y);
	pucSrc[3]=a;
}

